package handler;

import com.sun.net.httpserver.HttpExchange;
import constant.Constants;
import model.StakeManager;
import util.ResponseUtil;

import java.io.IOException;
import java.util.Map;
/**
 * @Description 获取投注top20
 * @Author admin
 * @Date 2024-12-25 11:45
 */
public class HighStakeHandler {
    private final StakeManager stakeManager;

    public HighStakeHandler(StakeManager stakeManager) {
        this.stakeManager = stakeManager;
    }

    public void doHandler(String betOfferIdStr, HttpExchange exchange) throws IOException {
        int betOfferId = Integer.parseInt(betOfferIdStr);
        Map<Integer, Integer> top20Stakes = stakeManager.getTop20StakesForOffer(betOfferId);
        if (top20Stakes.isEmpty()) {
            ResponseUtil.sendResponse(exchange, "The betting data is empty");
        } else {
            ResponseUtil.sendResponse(exchange, formatTop20Stakes(top20Stakes));
        }
    }

    private String formatTop20Stakes(Map<Integer, Integer> stakes) {
        StringBuilder response = new StringBuilder();
        for (Map.Entry<Integer, Integer> entry : stakes.entrySet()) {
            response.append(entry.getKey()).append(Constants.EQUAL_STR)
                    .append(entry.getValue()).append(Constants.COMMA_STR);
        }
        if (response.length() > 0) {
            response.deleteCharAt(response.length() - 1);
        }
        return response.toString();
    }
}